<!--
 代理商管理
-->

<template>
  <div class="lb-shop-order">
    <top-nav />
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="账号名称" prop="username">
            <el-input
              v-model="searchForm.username"
              placeholder="请输入账号名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="关联用户" prop="nickName">
            <el-input
              v-model="searchForm.nickName"
              placeholder="请输入关联用户"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-row class="page-top-operate">
        <lb-button
          size="medium"
          type="primary"
          icon="el-icon-plus"
          @click="addDialog = true"
          >{{ $t('menu.franchiseeAdd') }}</lb-button
        >
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="username" label="账号名称"></el-table-column>
        <el-table-column prop="passwd_text" label="账号密码"></el-table-column>
        <el-table-column prop="nickName" label="关联用户"></el-table-column>
        <el-table-column prop="balance" label="平台抽成">
          <template slot-scope="scope">
            <div>{{ scope.row.balance }}%</div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                type="primary"
                plain
                @click="editList(scope.row)"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
    <el-dialog
      :title="$t(accountForm.id ? 'menu.franchiseeEdit' : 'menu.franchiseeAdd')"
      :visible.sync="addDialog"
      width="600px"
      center
      :append-to-body="true"
    >
      <div class="f_r_c_c">
        <el-form
          :model="accountForm"
          :rules="rules"
          ref="ruleForm"
          label-position="left"
          label-width="90px"
          class="dialog-form"
        >
          <div class="f_r_m_c mb-10">
            <el-form-item label="账号:" prop="username">
              <el-input
                v-model="accountForm.username"
                placeholder="请输入账号"
              ></el-input>
            </el-form-item>
          </div>
          <div class="f_r_m_c mb-10">
            <el-form-item label="密码:" prop="passwd_text">
              <el-input
                v-model="accountForm.passwd_text"
                placeholder="请输入密码"
              ></el-input>
            </el-form-item>
          </div>

          <div class="f_r_m_c mb-10">
            <el-form-item label="平台抽成:" prop="balance">
              <el-input-number
                class="lb-input-number"
                :min="0"
                :max="100"
                :controls="false"
                v-model="accountForm.balance"
                placeholder="请输入提现比例"
              ></el-input-number>
              <block>%</block>
              <lb-tool-tips>
                <p class="mb-sm">提现比例取值0%到100%</p>
                <p class="mb-sm">
                  设置比例之后，代理商提现金额=实际金额-技师比例实际金额-分销商比例实际金额-平台提成比例*实际金额
                </p>
                <p class="mb-sm">
                  设置为0，表示平台不抽成，除去技师和分销商的提成后，剩下都为代理商的
                </p>
                <p class="mb-sm">
                  设置100，则表示代理商无提成，除去技师和代理商的提成后，剩下都归平台
                </p>
              </lb-tool-tips>
            </el-form-item>
          </div>
          <div class="f_r_m_c mb-10">
            <el-form-item label="关联用户:" prop="user_id">
              <el-tag @click="toShowDialog">{{
                accountForm.user_id ? accountForm.nickName : '选择关联用户'
              }}</el-tag>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialog = false">{{
          $t('action.cancel')
        }}</el-button>
        <el-button type="primary" @click="confirmAdd('ruleForm')">{{
          $t('action.comfirm')
        }}</el-button>
      </div>
    </el-dialog>

    <el-dialog title="关联用户" :visible.sync="userDialog" width="800px" center>
      <el-form
        :inline="true"
        :model="searchDialogForm"
        ref="searchDialogForm"
        label-width="70px"
      >
        <el-form-item label="输入查询" prop="nickName">
          <el-input
            v-model="searchDialogForm.nickName"
            placeholder="请输入用户昵称/手机号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <lb-button
            size="medium"
            type="primary"
            icon="el-icon-search"
            style="margin-right: 5px"
            @click="getUserData()"
            >{{ $t('action.search') }}</lb-button
          >
          <lb-button
            size="medium"
            icon="el-icon-refresh-left"
            style="margin-right: 5px"
            @click="resetForm('searchDialogForm')"
            >{{ $t('action.reset') }}</lb-button
          >
        </el-form-item>
      </el-form>
      <el-table
        :data="statusOptions"
        ref="singleTable"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        tooltip-effect="dark"
        height="40vh"
        style="width: 100%"
        highlight-current-row
        @current-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="用户ID"></el-table-column>
        <el-table-column prop="avatarUrl" label="头像">
          <template slot-scope="scope">
            <lb-image :src="scope.row.avatarUrl" />
          </template>
        </el-table-column>
        <el-table-column prop="nickName" label="昵称"></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchDialogForm.page"
        :pageSize="searchDialogForm.limit"
        :total="searchDialogForm.total"
        @handleSizeChange="
          (val) => {
            handleSizeChange(val, 'searchDialogForm')
          }
        "
        @handleCurrentChange="
          (val) => {
            handleCurrentChange(val, 'searchDialogForm')
          }
        "
      >
      </lb-page>
      <span slot="footer" class="dialog-footer">
        <el-button @click="userDialog = false">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      value: '',
      addDialog: false,
      loading: false,
      searchForm: {
        page: 1,
        limit: 10,
        username: '',
        nickName: ''
      },
      searchDialogForm: {
        page: 1,
        limit: 10,
        total: 0,
        nickName: ''
      },
      accountForm: {
        username: '',
        passwd_text: '',
        balance: '',
        user_id: ''
      },
      rules: {
        username: [
          {
            required: true,
            validator: this.$reg.isNoEmpty,
            reg_type: 2,
            text: '账号',
            trigger: 'blur'
          }
        ],
        passwd_text: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ],
        balance: [
          {
            required: true,
            message: '请输入分佣比例',
            trigger: 'blur'
          }
        ],
        user_id: [
          {
            required: true,
            message: '请选择关联用户',
            trigger: 'blur'
          }
        ]
      },
      statusOptions: [],
      tableData: [],
      total: 0,
      userDialog: false,
      query: '',
      currentRow: {}
    }
  },
  watch: {
    addDialog (val) {
      if (!val) {
        this.accountForm = {
          username: '',
          passwd_text: '',
          balance: '',
          user_id: ''
        }
        this.getTableDataList()
      }
    },
    userDialog (val) {
      if (!val) {
        this.getUserData()
      }
    }
  },
  created () {
    this.getTableDataList()
  },
  methods: {
    handleSelectionChange (val) {
      val = JSON.parse(JSON.stringify(val))
      let { id, nickName } = val
      val.nickName = nickName || `用户ID ${id}`
      this.currentRow = val
    },
    handleDialogConfirm () {
      let { id = 0, nickName = '' } = this.currentRow
      this.accountForm.user_id = id
      this.accountForm.nickName = nickName
      this.userDialog = false
    },
    toShowDialog () {
      this.userDialog = true
      this.getUserData()
    },
    resetForm (form) {
      this.range = ''
      this.$refs[form].resetFields()
      if (form === 'searchDialogForm') {
        this.getUserData()
      }
      this.getTableDataList(1)
    },
    handleSizeChange (val, type) {
      console.log('type', val, type)
      this[type].limit = val
      this.handleCurrentChange(1, type)
    },
    handleCurrentChange (val, type) {
      this[type].page = val
      if (type === 'searchForm') {
        this.getTableDataList()
      } else {
        this.getUserData()
      }
    },
    /**
     * @method 获取代理商列表
     */
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      this.$api.account.franchiseeList(searchForm).then((res) => {
        this.loading = false
        if (res.code === 200) {
          let { data, total } = res.data
          this.tableData = data
          this.total = total
        }
      })
    },
    getUserData () {
      this.$api.account.userList(this.searchDialogForm).then((res) => {
        if (res.code === 200) {
          this.statusOptions = res.data.data
          this.searchDialogForm.total = res.data.total
        }
      })
    },
    selChange (e) {
      this.accountForm.user_id = e
    },
    confirmDel (row) {
      this.$confirm(this.$t('tips.franchiseeDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }
      ).then(() => {
        this.$api.account.adminStatusUpdate({ id: row.id, status: '-1' }).then((res) => {
          this.$message.success(this.$t('tips.successOper'))
        })
      })
    },
    editList (row) {
      this.accountForm = {
        username: row.username,
        passwd_text: row.passwd_text,
        balance: row.balance,
        user_id: row.user_id,
        id: row.id,
        nickName: row.nickName
      }
      this.value = row.nickName
      this.addDialog = true
    },
    confirmAdd (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          let subForm = JSON.parse(JSON.stringify(this.accountForm))
          if (subForm.username.length < 6 || subForm.passwd_text.length < 6) {
            this.$message.error(subForm.username.length < 6 ? `账号不能少于6位数` : `密码不能少于6位数`)
            return
          }
          let methodModel = subForm.id ? 'adminUpdate' : 'adminAdd'
          delete subForm.nickName
          this.$api.account[methodModel](subForm).then((res) => {
            this.loading = false
            if (res.code === 200) {
              this.$message.success(this.$t(subForm.id ? `tips.successRev` : `tips.successOper`))
              this.addDialog = false
              this.accountForm = {
                username: '',
                passwd_text: '',
                balance: '',
                user_id: ''
              }
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.f_c {
  display: flex;
  flex-direction: column;
}
.f_r_m_c {
  display: flex;
  align-items: center;
}
.f_r_c_c {
  display: flex;
  justify-content: center;
  align-items: center;
}
.f1 {
  display: flex;
  flex: 1;
}
.mr-10 {
  margin-right: 10px;
}
.mb-10 {
  margin-bottom: 10px;
}
.ml-10 {
  margin-left: 10px;
}
.w_60 {
  width: 60px;
}
.dialog-form {
  .el-input,
  .el-input-number {
    width: 300px;
  }
}
</style>

